package com.ly.core.web.action;

import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.DefaultActionSupport;

import com.ly.core.engine.MailEngine;
import com.ly.core.web.paging.PagingBean;

/**
 * Ext Base Action for all the request.
 * 
 * @author csx
 * 
 */
public class BaseAction extends DefaultActionSupport {

	/**
	 * 结合Ext的分页功能： dir DESC limit 25 sort id start 50
	 */
	/**
	 * 当前是升序还是降序排数据
	 */
	protected String dir;
	/**
	 * 排序的字段
	 */
	protected String sort;
	/**
	 * 每页的大小
	 */
	protected Integer limit = 25;
	/**
	 * 开始取数据的索引号
	 */
	protected Integer start = 0;

	protected String jsonString;

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public String getJsonString() {
		return jsonString;
	}

	public BaseAction() {
		this.setSuccessResultValue("/jsonStruts.jsp");
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected transient final Log logger = LogFactory.getLog(getClass());

	protected MailEngine mailEngine;

	// protected Integer pageSize=PagingBean.DEFAULT_PAGE_SIZE;
	//	
	// protected Integer curPage=1;

	public final String CANCEL = "cancel";

	public final String VIEW = "view";

	/**
	 * Convenience method to get the request
	 * 
	 * @return current request
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * Convenience method to get the response
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * Convenience method to get the session. This will create a session if one
	 * doesn't exist.
	 * 
	 * @return the session from the request (request.getSession()).
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	// ---------------------------Methods------------------------------

	protected PagingBean getInitPagingBean() {
		PagingBean pb = new PagingBean(start, limit);
		// getRequest().setAttribute(PagingBean.PAGING_BEAN, pb);
		return pb;
	}

	public void setMailEngine(MailEngine mailEngine) {
		this.mailEngine = mailEngine;
	}

	// public void setPageSize(int pageSize) {
	// this.pageSize = pageSize;
	// }
	//
	// public void setCurPage(int curPage) {
	// this.curPage = curPage;
	// }

	public String list() {
		return SUCCESS;
	}

	public String edit() {
		return INPUT;
	}

	public String save() {
		return INPUT;
	}

	public String delete() {
		return SUCCESS;
	}

	public String multiDelete() {
		return SUCCESS;
	}

	public String multiSave() {
		return SUCCESS;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	/**
	 * 向客户端写文本
	 * **/
	public void getMessage(HttpServletResponse response, String message) {
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			out.write(message);
			out.flush();
			out.close();
		} catch (Exception e) {
			if (out != null) {
				out.close();
			}
		}
	}
	/**由form提交的信息转换成object对象
	 * **/
	public Object getObjectFromJson(HttpServletRequest request,
			HttpServletResponse response, java.io.Serializable obj)
			throws Exception {
		logger.info(request.getRequestURI());
		request.setCharacterEncoding("utf-8");
		Enumeration en = request.getParameterNames();
		String json = "";
		for (; en.hasMoreElements();) {
			String k = (String) en.nextElement();
			String v = request.getParameter(k);
			if (v != null)
				json += "\"" + k + "\":\"" + v.trim() + "\",";
			else
				json += "\"" + k + "\":\"\",";
		}
		json = "{" + json + "}";
		logger.info(json);
		JSONObject jsonObj = JSONObject.fromObject(json);
		Object object = JSONObject.toBean(jsonObj, Class
				.forName(obj.toString()));
		logger.info(object);
		return object;
	}

	public String getJsonFromJSONArray(Object map) {
		JSONArray jsonObject = JSONArray.fromObject(map);
		String json = jsonObject.toString();
		logger.info(json);
		return json;
	}

	public String getJsonFromJSONObject(Object map) {
		JSONObject jsonObject = JSONObject.fromObject(map);
		String json = jsonObject.toString();
		logger.info(json);
		return json;
	}
}
